<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2018 http://www.donglixia.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: 十万马 <962863675@qq.com>
// +----------------------------------------------------------------------
// | DateTime: 2018-02-09 16:17
// +----------------------------------------------------------------------

namespace app\index\model;

use app\common\model\Article as CoreArticle;
use think\Db;
use think\Request;

class Article extends CoreArticle
{
    /**
     * 默认需要获取的字段
     * @var string
     */
    private $field_art = 'art_id,cat_id,title,title_sub,link,keywords,author,level,img_url,read_num,praise_num,describe,content,address,people_num,public_time';

    /**
     * 默认需要获取的字段
     * @var string
     */
    private $language = 1;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $request = Request::instance();
        if ($request->module() == 'english') {
            $this->language = 2;
        } elseif ($request->module() == 'tradi') {
            $this->language = 3;
        }
    }

    /**
     * 获取数据列表
     * @param array $map
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getDataList($map = [])
    {
        $param = $this->request->param();
        $limit = !empty($param['limit']) ? $param['limit'] : 15 ;

        $map['language'] = $this->language;//语言
        $map['state'] = 1;//已发布状态
        $this->field_art .= ',public_time as public_date';
        return $this->where($map)->field($this->field_art)
            ->order('sort_num asc, art_id desc')
            ->paginate($limit, false, ['query'=>$param]);
    }

    /**
     * 获取分类列表
     * @param array $map
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getCatList($map = [])
    {
        $map['language'] = $this->language;//语言
        $map['state'] = 1;//已发布状态
        $list = Db::name('article_cat')->where($map)->field('cat_id,cat_name')->order('sort_num asc, cat_id desc')->select();
        return $list;
    }

    /**
     * 获取分类数据列表
     * @param array $map
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function getLocationTree($map = [])
    {
        $param = $this->request->param();
        $curr_cat_id = !empty($param['cat_id']) ? $param['cat_id'] : 5;

        $map['language'] = $this->language;//语言
        $map['state'] = 1;//状态：启用
        $list = Db::name('article_cat')->where($map)->field('cat_id,pid,cat_name')->order('sort_num asc, cat_id desc')->select();
        $list = $this->currCatDisplay($list, $curr_cat_id);
        $list = \Tree::getTree($list, 'cat_id', 'pid');
        return $list;
    }

    /**
     * 标记当前分类及父级分类为显示状态
     * @param $list
     * @param int $cat_id
     * @return mixed
     */
    protected function currCatDisplay($list, $cat_id = 1)
    {
        $curr_cat_ids = get_parent_ids($cat_id, 'article_cat');//当前分类父级id集合
        foreach ($list as $k=>$v) {
            $list[$k]['is_display'] = in_array($v['cat_id'], $curr_cat_ids) ? 1 : 0;
        }
        return $list;
    }

    /**
     * 获取详情
     * @param array $map
     * @return array|bool|false|\PDOStatement|string|\think\Model
     */
    public function getArticleInfo($map = [])
    {
        if (empty($map)) {
            return false;
        }
        $res = Db::name('article')->where($map)->field($this->field_art)->find();
        if (empty($res)) {
            return false;
        }
        $res['content'] = !empty($res['content']) ? htmlspecialchars_decode($res['content']) : '';
        $res['public_time'] = !empty($res['public_time']) ? date('Y-m-d H:i:s', $res['public_time']) : '0000-00-00';
        return $res;
    }

    /**
     * 获取最近文章列表
     * @param array $map
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getLatestNews($map = [])
    {
        $map['language'] = $this->language;//语言
        $map['state'] = 1;//已发布状态
        $list = Db::name('article')->where($map)->field($this->field_art)->limit(0,15)->order('art_id desc')->select();
        return $list;
    }

    /**
     * 根据版块cat_type，获取cat_id
     * @param array $map
     * @return array
     */
    public function getCatIdByCatType($map = [])
    {
        $map['state'] = 1;//已发布状态
        $map['language'] = $this->language;//语言
        return Db::name('article_cat')->where($map)->column('cat_id');
    }

    /**
     * 获取某一位置banner
     * @param $cat_code
     * @param $basic_code
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getBanner($cat_code = 'banner', $basic_code = 'AB')
    {
        $basic_pid = Db::name('basic_info')->where('cat_code', $cat_code)->where('basic_code', $basic_code)->value('basic_id');
        return Db::name('basic_banner')
            ->where(['location'=>$basic_pid, 'language'=>$this->language, 'state'=>1])
            ->field('banner_id,img_name,img_name_style,img_url,link,describe')
            ->find();
    }
}